package com.tz.crm.controller.system;

import com.tz.crm.entity.Department;
import com.tz.crm.service.DepartmentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 顾梦莹
 * Date: 2017-3-14.
 * Time: 下午 4:31
 */
@Controller
@RequestMapping("/dept")
public class DepartmentController {
    @Resource
    private DepartmentService departmentService;

    @RequestMapping("/getDepartment")
    public String getDepartment(String deptId, Model model){
        Department department=new Department();
        if(null!=deptId && deptId.length()>0){
            department=departmentService.getById(deptId);
        }
        model.addAttribute("department",department);
        return "searchDept";
    }

    @RequestMapping("/initAdd")
    public String initAdd(Model model){
        List<Department> departmentList=departmentService.getAll();
        model.addAttribute("departments",departmentList);
        return "addDept";
    }

    @RequestMapping("/add")
    public String add(String deptId,String deptDeptName,String deptRemark){
        Department department=new Department();
        Department parent=departmentService.getById(deptId);
        department.setDeptName(deptDeptName);
        department.setRemark(deptRemark);
        department.setParent(parent);
        departmentService.save(department);
//        return "redirect:/user/search";
        return "redirect:/system/search";
    }

    @RequestMapping("/initUpdate")
    public String initUpdate(String deptId,Model model){
        Department department=departmentService.getById(deptId);
        model.addAttribute("department",department);
        List<Department> departmentList=departmentService.getAll();
        model.addAttribute("departments",departmentList);
        return "updateDept";
    }

    @RequestMapping("/update")
    public String update(String deptId,String deptDeptName,String deptRemark,String parentId){
        Department department=departmentService.getById(deptId);
        department.setDeptName(deptDeptName);
        department.setRemark(deptRemark);
        Department parent=departmentService.getById(parentId);
        //如果原部门本身就没有父部门即原部门是顶层部门，则不修改它的新父部门属性
        //如果选择的父部门是要修改的原部门，也不修改它的新部门属性
        if(null!=department.getParent() || department.getId()==parent.getId()) {
            department.setParent(parent);
        }
        departmentService.update(department);
//        return "redirect:/user/search";
        return "redirect:/system/search";
    }

    @RequestMapping(value = "/delete",produces = "application/json;charset=utf-8")
    @ResponseBody
    public boolean delete(String deptId){
        Department department=departmentService.getById(deptId);
        if(null==department.getParent()){
            return false;
        }else{
            departmentService.delete(department);
            return true;
        }
    }
}
